package com.yang.studentsapp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LongWH on 2016/10/9.
 * All Rights Reserved by ZhiYou @2016 - 2017
 * adapter 的基础类，没有复写 getView 方法
 */
public abstract class CommonBaseAdapter<T> extends BaseAdapter{
    protected List<T> mDataList = new ArrayList<>();
    protected LayoutInflater mInflater;
    protected Context mContext;


    public CommonBaseAdapter(Context context, List<T> dataList) {
        mContext = context;
        if (dataList != null) {
            mDataList.addAll(dataList);
        }
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public T getItem(int position) {
        T data = null;
        if (position >= 0 && position < getCount()) {
            data = mDataList.get(position);
        }
        return data;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void update(List<T> data) {
        mDataList.clear();
        if (data != null) {
            mDataList.addAll(data);
        }
        notifyDataSetChanged();
    }
}
